package com.xmz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmz.entity.Book;
import com.xmz.entity.Author;
import com.xmz.mapper.BookMapper;
import com.xmz.mapper.AuthorMapper;
import com.xmz.service.BookService;
import com.xmz.util.PageResult;
import com.xmz.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 图书服务实现类
 * </p>
 *
 * @since 2025-01-13
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private AuthorMapper authorMapper;

    /**
     * 实现分页条件查询
     * @param pageNo 当前页号
     * @param pageSize 页面大小
     * @param bookVo 查询信息
     * @return 分页信息
     */
    @Override
    public PageResult<Book> loadList(Integer pageNo, Integer pageSize, BookVo bookVo){
        PageResult<Book> pageResult = new PageResult<>();
        Page<Book> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

        if(bookVo != null) {
            queryWrapper.like(StrUtil.isNotBlank(bookVo.getTitle()), "title", bookVo.getTitle())
                    .eq(bookVo.getPublicationYear() != null, "publication_year", bookVo.getPublicationYear());

            if( !StrUtil.isEmpty(bookVo.getAuthorName()) && StrUtil.isNotBlank(bookVo.getAuthorName())){
                queryWrapper.eq("author_id",getAuthorIdByName(bookVo.getAuthorName()));
            }
        }

        IPage<Book> result = bookMapper.selectPage(page, queryWrapper);
        List<Book> records = result.getRecords();

        // 查询所有的作者名字
        records.forEach(item -> {
            item.setAuthorName(getAuthorNameById(item.getAuthorId()));
        });

        pageResult.setRows(records);
        pageResult.setTotal(result.getTotal());
        return pageResult;
    }

    @Override
    public int addBook(Book book) {
        book.setAuthorId(getAuthorIdByName(book.getAuthorName()));
        return bookMapper.insert(book);
    }

    @Override
    public int deleteBook(List<Integer> ids) {
        return bookMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateBook(Book book) {
        book.setAuthorId(getAuthorIdByName(book.getAuthorName()));
        return bookMapper.updateById(book);
    }

    /**
     * 根据作者名称获取id
     * @param name 作者名称
     * @return 作者id
     */
    private Integer getAuthorIdByName(String name) {
        Author author = authorMapper
                .selectOne(new QueryWrapper<Author>().eq("name", name));
        return author != null ? author.getAuthorId() : null;
    }

    /**
     * 根据作者id获取作者名称
     * @param id 作者id
     * @return 作者名称
     */
    private String getAuthorNameById(Integer id) {
        Author author = authorMapper.selectById(id);
        return author != null ? author.getName() : null;
    }
}
